package top.lyc829.iot_android.fragment.profile;

import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SPUtils;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xpage.model.PageInfo;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.textview.supertextview.SuperButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import top.lyc829.iot_android.R;
import top.lyc829.iot_android.activity.LoginActivity;
import top.lyc829.iot_android.adapter.ProfilePageAdapter;
import top.lyc829.iot_android.core.BaseFragment;
import top.lyc829.iot_android.core.LazyFragment;
import top.lyc829.iot_android.util.Utils;

@Page(name = "ProfileFragment")
public class ProfileFragment extends LazyFragment {

    @BindView(R.id.profile_recyclerview)
    RecyclerView profileRecycler;

    @BindView(R.id.btn_logout)
    SuperButton btnLogout;

    ProfilePageAdapter adapter;


    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setTitle("我的")
                .disableLeftView();
        return titleBar;
    }

    public static ProfileFragment newInstance() {
        return new ProfileFragment();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.profile_fragment;
    }

    @Override
    protected void initViews() {
        WidgetUtils.initGridRecyclerView(profileRecycler, 3, DensityUtils.dp2px(2));
        adapter = new ProfilePageAdapter();

        List<PageInfo> pageInfos = new ArrayList<>();
        pageInfos.add(new PageInfo(
                "指纹",
                "FingerFragment",
                "{\"\":\"\"}",
                CoreAnim.slide,
                R.drawable.finger
        ));
        pageInfos.add(new PageInfo(
                "卡片",
                "NfcFragment",
                "{\"\":\"\"}",
                CoreAnim.slide,
                R.drawable.door_open
        ));
        pageInfos.add(new PageInfo(
                "室友",
                "FriendFragment",
                "{\"\":\"\"}",
                CoreAnim.slide,
                R.drawable.friend
        ));
        adapter.setList(pageInfos);
        profileRecycler.setAdapter(adapter);
    }

    @Override
    protected void initListeners() {
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            PageInfo item = adapter.getItem(position);
            if (item != null) {
                openNewPage(item.getClassPath(), ProfileActivity.class);
            }
        });

        btnLogout.setOnClickListener(v -> {
            SPUtils.getInstance().remove("token");
            Utils.showToast("退出登录成功");
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        });

    }

    @Override
    public void lazyInit() {

    }
}